import re

import requests
from bs4 import BeautifulSoup
from flask import jsonify, render_template, request, current_app
import math
from information.utils.response_code import RET
from . import profile_blu
from .. import data,Page_MAX

@profile_blu.route('/base_info',methods=['GET'])
def base_info():
    if not data['user']:
        return render_template('news/404.html',data=data)
    from information.utils.models import User
    try:
        user=User.query.filter_by(id=data['user']['id']).first().to_dict()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='数据查询失败')
    data['user']=user
    print(user)
    return render_template('news/user_base_info.html',data=data)

@profile_blu.route('/pic_info',methods=['GET'])
def pic_info():
    if not data['user']:
        return render_template('news/404.html',data=data)
    from information.utils.models import User
    try:
        user = User.query.filter_by(id=data['user']['id']).first()
        data['user']=user.to_dict()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='数据查询失败')
    return render_template('news/user_pic_info.html',data=data)

@profile_blu.route('/user_follow',methods=['GET'])
def user_follow():
    from information.utils.models import User
    data['users']=[]
    follows=[]
    try:
        users=User.query.filter_by(id=data['user']['id']).first().followers
        for user in users:
            follows.append(user.to_dict())
        data['total_page'] = math.ceil(len(follows) / Page_MAX)
        data['users'] = []
        data['users'] = follows[0:Page_MAX]
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='数据查询失败')
    return render_template('news/user_follow.html',data=data)

@profile_blu.route('/pass_info',methods=['GET'])
def pass_info():
    if not data['user']:
        return render_template('news/404.html',data=data)
    return render_template('news/user_pass_info.html')

@profile_blu.route('/user_collection',methods=['GET'])
def user_collection():
    from information.utils.models import News,tb_user_collection,User
    data['collections']=[]
    collections_list=[]
    try:
        collections=User.query.filter_by(id=data['user']['id']).first().collection_news
        for collection in collections:
            collections_list.append(collection.to_review_dict())
        data['total_page'] = math.ceil(len(collections_list) / Page_MAX)
        data['collections'] = []
        data['collections'] = collections_list[0:Page_MAX]
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='数据查询失败')
    return render_template('news/user_collection.html',data=data)

@profile_blu.route('/news_release',methods=['GET'])
def news_release():
    from information.utils.models import Category
    try:
        category_li = Category.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='数据查询失败')
    category_lis = []
    for ca in category_li:
        category_lis.append(ca.to_dict())
    data['categories']=category_lis
    return render_template('news/user_news_release.html',data=data)

@profile_blu.route('/user_news_list',methods=['GET'])
def user_news_list():
    from information.utils.models import User
    data['news_list']=[]
    news_list=[]
    try:
        news=User.query.filter_by(id=data['user']['id']).first().news_list
        for new in news:
            #data['news_list'].append(new.to_review_dict())
            news_list.append(new.to_review_dict())
        data['total_page'] = math.ceil(len(news_list) / Page_MAX )
        data['news_list'] = []
        data['news_list'] = news_list[0:Page_MAX]
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='数据查询失败')
    return render_template('news/user_news_list.html',data=data)

@profile_blu.route('/other_info',methods=['GET'])
def other_info():
    from information.utils.models import User,Category
    user_id = int(request.args.get('user_id'))
    data['news_list'] = []
    news_list = []
    data['other_info'] = None
    try:
        other = User.query.filter_by(id=user_id).first()
        news=other.news_list
        for new in news:
            news_list.append(new.to_review_dict())
        data['total_page'] = math.ceil(len(news_list) / Page_MAX)
        data['news_list'] = []
        data['news_list'] = news_list[0:Page_MAX]
        user=User.query.filter_by(id=data['user']['id']).first()
        data['other_info']=other.to_dict()
        category_li = Category.query.all()
        category_lis = []
        for ca in category_li:
            category_lis.append(ca.to_dict())
        data['category_li'] = category_lis
        data['is_followed']=None
        followers=user.followers
        for follower in followers:
            if int(user_id)==follower.id:
                data['is_followed']=True
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg='数据查询失败')
    return render_template('news/other.html',data=data)